package xin.handler;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import xin.anotation.Permissions;
import xin.entity.TAuthPermission;
import xin.service.TAuthPermissionService;

import javax.annotation.Resource;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@Component
public class HandlerPermission implements ApplicationContextAware {
    @Resource
    private TAuthPermissionService permissionService;


    @Resource
    private ApplicationContext applicationContext;

    private List<Class> classList = new ArrayList<>();
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext=applicationContext;
        String[] beanDefinitionNames = this.applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            Object bean = applicationContext.getBean(beanDefinitionName);
            Class<?> aClass = bean.getClass();
            if (aClass.getAnnotation(Permissions.class)!=null){
                classList.add(aClass);
            }
        }
        this.createPermission();
    }

    /**
     * 创建permission List集合
     */
    public void createPermission(){
        //清除所有数据
        this.permissionService.getBaseMapper().delete(null);
        for (Class aClass : classList) {
            Permissions annotation = (Permissions) aClass.getAnnotation(Permissions.class);
            if (annotation!=null){
                Annotation annotation1 = aClass.getAnnotation(RequestMapping.class);
                RequestMapping requestMapping = (RequestMapping) annotation1;
                TAuthPermission permission = new TAuthPermission();

                permission.setName(annotation.name());
                permission.setSn(annotation.sn());
                permission.setUrl(requestMapping.value()[0]);
                permission.setParentId(null);
                //保存父权限
                this.permissionService.save(permission);
                Method[] declaredMethods = aClass.getDeclaredMethods();
                for (Method declaredMethod : declaredMethods) {
                    Permissions permissions = declaredMethod.getAnnotation(Permissions.class);
                    TAuthPermission item = null;
                    if (permissions!=null){
                        item = new TAuthPermission();
                        item.setUrl(getUrl(declaredMethod.getAnnotations()));
                        item.setName(permissions.name());
                        item.setSn(permissions.sn());
                        item.setParentId(permission.getId());
                        this.permissionService.save(item);
                    }

                }
            }
        }
    }

    /**
     * 获取请求地址
     * @param annotations
     * @return
     */
    public String getUrl(Annotation[] annotations){
        for (Annotation annotation : annotations) {
            if (annotation instanceof RequestMapping){
                RequestMapping requestMapping = (RequestMapping) annotation;
                return  requestMapping.value()[0];
            }
            else if (annotation instanceof GetMapping){
                GetMapping getMapping = (GetMapping) annotation;
                return getMapping.value()[0];
            }
            else if (annotation instanceof PostMapping){
                PostMapping postMapping = (PostMapping) annotation;
                return postMapping.value()[0];
            }
            else if (annotation instanceof DeleteMapping){
                DeleteMapping deleteMapping = (DeleteMapping) annotation;
                return deleteMapping.value()[0];
            }
            else if (annotation instanceof PutMapping){
                PutMapping putMapping = (PutMapping) annotation;
                return putMapping.value()[0];
            }
        }
        return  null;

    }




}
